package com.fenghongzhang.one;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager vp;

    private MySqlHelp mySqlHelp;
    private SQLiteDatabase readableDatabase;
    private String flag = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        mySqlHelp = new MySqlHelp(this, "tabletest.db", null, 1);
        readableDatabase = mySqlHelp.getReadableDatabase();

        //通过sp来判断是否启动过.
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean isOpen = welcome.getBoolean("isOpen", false);
        if(isOpen){
            //2,取值
            Cursor table1 = readableDatabase.query("table1", null, null, null, null, null, null);
            if(table1 != null){

//                table1.moveToFirst();
                while(table1.moveToNext()){
                   flag  = table1.getString(table1.getColumnIndex("flag")); //100

                }
                table1.close();
            }

            if(flag.equals("100")){
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
                return;
            }

        }


        //更改sp的内容
        SharedPreferences.Editor edit = welcome.edit();
        edit.putBoolean("isOpen",true);
        edit.commit();

        //1,插入
        ContentValues contentValues = new ContentValues();
        contentValues.put("flag",100);
        readableDatabase.insert("table1",null,contentValues);



        //取到了标志,就直接跳转
        /**
         * 从数据库中.
         * 1, 首选有数据库.
         * 2, 有表.
         * 3, 表里有数据库.
         * 4, 启动欢迎页以后插入,然后在取值.
         */

//        if(){

//        }

        vp = (ViewPager) findViewById(R.id.vp);

        List<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());

        VpAdapter vpAdapter = new VpAdapter(getSupportFragmentManager(),list);

        vp.setAdapter(vpAdapter);

    }
}
